package com.yuanchu.marketing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.common.utils.DateFormatUtils;
import com.yuanchu.common.utils.PageResult;
import com.yuanchu.marketing.client.FeignStoreClient;
import com.yuanchu.marketing.dao.YoshopUserPointsLogDao;
import com.yuanchu.marketing.entity.YoshopUserEntity;
import com.yuanchu.marketing.entity.YoshopUserPointsLogEntity;
import com.yuanchu.marketing.service.YoshopUserPointsLogService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Service("yoshopUserPointsLogService")
public class YoshopUserPointsLogServiceImpl extends ServiceImpl<YoshopUserPointsLogDao, YoshopUserPointsLogEntity> implements YoshopUserPointsLogService {

    @Resource
    private FeignStoreClient feignStoreClient;

    @Override
    public PageResult<YoshopUserPointsLogEntity> queryUserPointsLog(Integer userId, Integer page) {
        LambdaQueryWrapper<YoshopUserPointsLogEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YoshopUserPointsLogEntity::getUserId, userId);
        // 全部
        if (page == null || page == 0) {
            page = 1;
        }
        lqw.orderByDesc(YoshopUserPointsLogEntity::getCreateTime);
        Page<YoshopUserPointsLogEntity> page1 = new Page<>(page, 15);
        Page<YoshopUserPointsLogEntity> page_result = this.page(page1, lqw);
        List<YoshopUserPointsLogEntity> records = page_result.getRecords();
        records.forEach(userLog -> userLog.setCreateTime(DateFormatUtils.formatDatetime(userLog.getCreateTime())));

        PageResult<YoshopUserPointsLogEntity> result = new PageResult<>();
        result.setTotal(Math.toIntExact(page_result.getTotal()));
        result.setCurrentPage(page);
        result.setData(records);
        result.setPerPage(15);
        long total = page_result.getTotal();
        // 计算总页数，向下取整避免超出实际页数（这里已假设pageInfo.getTotal()返回的是long类型）
        int totalPages = (int) Math.ceil((double) total / 15);
        result.setLastPage(totalPages);

        return result;
    }

    @Override
    public PageResult<YoshopUserPointsLogEntity> queryUserPointsLog(Integer page, String search, String betweenTimeStart, String betweenTimeEnd) {
        LambdaQueryWrapper<YoshopUserPointsLogEntity> lqw = new LambdaQueryWrapper<>();
        // 全部
        if (page == null || page == 0) {
            page = 1;
        }
        if (StringUtils.isNotEmpty(betweenTimeStart) && StringUtils.isNotEmpty(betweenTimeEnd)) {
            long start = DateFormatUtils.convertStringToSeconds(betweenTimeStart);
            long end = DateFormatUtils.convertStringToSeconds(betweenTimeEnd);
            lqw.between(YoshopUserPointsLogEntity::getCreateTime, start, end);
        }
        lqw.orderByDesc(YoshopUserPointsLogEntity::getCreateTime);
        Page<YoshopUserPointsLogEntity> page1 = new Page<>(page, 15);
        Page<YoshopUserPointsLogEntity> page_result = this.page(page1, lqw);
        List<YoshopUserPointsLogEntity> records = page_result.getRecords();
        records.forEach(userLog -> {
            userLog.setCreateTime(DateFormatUtils.formatDatetime(userLog.getCreateTime()));
            YoshopUserEntity user = feignStoreClient.queryUserByUserId(userLog.getUserId());
            if (user != null){
                userLog.setUser(user);
            }
        });
        if (StringUtils.isNotEmpty(search)) {
            records = records.stream().filter(userLog -> userLog.getUser().getNickName().contains(search)).collect(Collectors.toList());
        }

        return PageResult.formatPageResult(records, page, 15, page_result.getTotal());
    }
}